package yl.hs.bmipfull.service.material.impl;

import org.springframework.beans.factory.annotation.Autowired;
import yl.hs.bmipfull.annotations.DBTable;
import yl.hs.bmipfull.daos.material.IPurchaseSysTagsDao;
import yl.hs.bmipfull.daos.material.IPurchaseSystemLogDao;
import yl.hs.bmipfull.daos.system.ICoreDataTagDao;
import yl.hs.bmipfull.daos.system.IDutyInfoDao;
import yl.hs.bmipfull.daos.system.IOrganizationInfoDao;
import yl.hs.bmipfull.pojo.material.PurchaseSysTagsEntity;
import yl.hs.bmipfull.pojo.material.PurchaseSystemLogEntity;
import yl.hs.bmipfull.pojo.system.CoreDataTagEntity;
import yl.hs.bmipfull.pojo.system.DutyInfoEntity;
import yl.hs.bmipfull.pojo.system.OrganizationInfoEntity;
import yl.hs.bmipfull.pojo.system.UserInfoEntity;
import yl.hs.bmipfull.service.IBaseService;
import yl.hs.bmipfull.service.impl.BaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.OrganizationUnitType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * PurchaseBaseService
 *
 * @author 谢景广
 * @category
 * @date 2021-05-25
 */
public class PurchaseBaseService extends BaseService implements IBaseService {
    @Autowired
    IPurchaseSysTagsDao tagsDao;
    @Autowired
    IOrganizationInfoDao orgDao;
    @Autowired
    ICoreDataTagDao coreTagDao;
    @Autowired
    IDutyInfoDao dutyDao;
    @Autowired
    protected IPurchaseSystemLogDao logDao;

    @Override
    public Integer writeLog(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        var user = HsApp.getCurrent();
        var log = new PurchaseSystemLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }


        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);
        switch (action) {
            case PRINT -> content = String.format("%s打印了编号为%s的%s信息", userName, dataSN, objectName);
            case CREATE -> content = String.format("%s创建了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
            case UPDATE -> content = String.format("%s更新了编号为%s的%s信息:%s", userName, dataSN, objectName, content);
            case DELETE -> content = String.format("%s删除了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
            case EXPORT -> content = String.format("%s导出了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
            case IMPORT -> content = String.format("%s导入了编号为%s的%s信息", userName, dataSN, objectName);
            case QUERY -> content = String.format("%s查询了编号为%s的%s信息", userName, dataSN, objectName);
            case INVALID -> content = String.format("%s作废了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
            case REVIEWED -> content = String.format("%s审核了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
            case UNREVIEWED -> content = String.format("%s取消编号为%s的%s信息审核：%s", userName, dataSN, objectName,content);
            default -> content = String.format("%s操作了编号为%s的%s信息:%s", userName, dataSN, objectName,content);
        }
        log.setContent(content);
        log.setDataSN(dataSN);
        log.setDataJson(dataJson);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        log.setUserName(user.getUserName());
        return logDao.insertNew(log);
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }

    @Override
    public void forWriteLog(String[] snList, short state) {
        var user = HsApp.getCurrent();
        if (state == -2) {
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"彻底删除了数据编号为"+str+"的数据信息","",null);
            }
        }else{
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"恢复删除了数据编号为"+str+"的数据信息","",null);
            }
        }
    }

    public Integer writeLog(ActionType action, String[] snArray, Class<?> classType) {
        String objectName = "信息";
        var user = HsApp.getCurrent();

        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        var log = new PurchaseSystemLogEntity();
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        log.setUserSN(userSN);
        var content = String.format("%s删除了编号为(%s)的%s信息", userName, String.join(",", snArray), objectName);
        log.setContent(content);
        log.setDataSN(String.join(",", snArray));
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }

    public void writeUserTag(String dataSN, Class<?> classType, String relationType) {

        //String typeCode, String title,
        var user = HsApp.getCurrent();
        if (user != null) {
            var mapCode = Tl.getDBMapCode(classType);
            PurchaseSysTagsEntity tag = new PurchaseSysTagsEntity();
            tag.setMainSN(dataSN);
            tag.setMainType(mapCode);
            tag.setTagSN(user.getUserSN());
            tag.setTagTitle(user.getUserName());

            tag.setTagType(Tl.getDBMapCode(UserInfoEntity.class));
            tag.setState((short) 0);
            tag.setRelationType(relationType);
            tagsDao.insertNew(tag);
            var userUnits = user.getOrganizationCode().split(",");
            var unitList = orgDao.queryBySnArray(userUnits);
            var orgCode = Tl.getDBMapCode(OrganizationInfoEntity.class);
            for (var unit : unitList) {
                tag.setTagType(orgCode);
                tag.setTagTitle(unit.getFullName());
                tag.setTagSN(unit.getSn());
                tagsDao.insertNew(tag);
            }
        }

    }



    public List<String> getDepartArraySN() {
        var user = HsApp.getCurrent();
        var st = new ArrayList<String>();
        if (user != null) {
            if(user.isAdmin()){
                var list=user.getOrganizationCode().split(",");
                Collections.addAll(st, list);
                return st;
            }
            else
            {
                var dutyList = getUserDutyList(user.getUserSN());
                if(dutyList.size() > 0){
                    for (DutyInfoEntity duty : dutyList) {
                        if (!st.contains(duty.getUnitSN())) {
                            st.add(duty.getUnitSN());
                        }
                    }
                }
                return st;
            }
        }
        return st;
    }

    public List<String> getCompanySNList(){
        var userSN = HsApp.getCurrent().getUserSN();
        var s = coreTagDao.queryBySN(new String[]{userSN});
        var orgCode = Tl.getDBMapCode(OrganizationInfoEntity.class);
        var orgList = orgDao.queryBySnArray(s.stream().filter(p -> p.getTagType().equals(orgCode) && p.getMainSN().equals(userSN)).map(CoreDataTagEntity::getTagSN).toArray(String[]::new));
        var allOrgSNList = new ArrayList<String>();
        for (OrganizationInfoEntity organizationInfoEntity : orgList) {
            var orgCodes = organizationInfoEntity.getParentCode().split(",");
            if (orgCodes.length > 0) {
                for (String code : orgCodes) {
                    if (!allOrgSNList.contains(code)) {
                        allOrgSNList.add(code);
                    }
                }
            }
        }
        var allOrgList = orgDao.queryBySnArray(allOrgSNList.toArray(new String[0]));
        allOrgSNList.clear();
        if (allOrgList.size()>0){
            for (OrganizationInfoEntity org : allOrgList) {
                if (org.getOrganizationType() == OrganizationUnitType.Company.code) {
                    if (!allOrgSNList.contains(org.getSn())) {
                        allOrgSNList.add(org.getSn());
                    }
                }
            }
        }
        return  allOrgSNList;
    }
}
